/*!
 * utils
 * Copyright(c) 2022 Jason Bai
 * MIT Licensed
 */

/**
 * 判断浏览器信息（提取自vue.js源码）
 * @return {Boolean}
 */
const UA = window.navigator.userAgent.toLowerCase();
const isIE = /msie|trident/.test(UA)
const isEdge = UA.indexOf('edge/') > 0
const isAndroid = UA.indexOf('android') > 0
const isIOS = /iphone|ipad|ipod|ios/.test(UA)
const isChrome = /chrome\/\d+/.test(UA) && !isEdge
const isFF = UA.match(/firefox\/(\d+)/)
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(UA)

/**
 * hello world
 * @param {String} [msg]
 */
function hello(msg = 'Jason Bai') {
    console.log(`hello ${msg}`)
}

/**
 * 精确的判断数据类型
 * @param {*} value
 * @return {String}
 */
function getType(value) {
    return Object.prototype.toString.call(value).slice(8, -1).toLowerCase()
}

/**
 * 数字格式化为千分符
 * @param {Number} num
 * @return {String}
 */
function formatNum(num) {
    return Number(num).toLocaleString()
}

/**
 * 生成随机字符串
 * @return {String} 
 */
function randomStr() {
    return Math.random().toString(36).slice(-8);
}

/**
 * 生成随机颜色
 * @return {String} 
 */
function randomColor() {
    return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);
}

/**
 * 获取URL中的查询参数对象
 * @param {String} url
 * @return {Object}
 */
function parseUrlParams(url) {
    const urlObj = new URL(url)
    const queryParams = new URLSearchParams(urlObj.search);
    return Object.fromEntries(queryParams);
}

/**
 * 根据传入的对象生成查询参数
 * @param {Object} obj
 * @return {String}
 */
function generateUrlParams(obj) {
    const params = new URLSearchParams(obj);
    return params.toString();
}

/**
 * 只执行一次函数
 * @param {Function} func
 * @return {Function} 
 */
function once(func) {
    let isExed = false
    return function () {
        if (!isExed) {
            func();
            isExed = true;
        }
    }
}

/**
 * 防抖
 * @param {Function} func
 * @param {Number} [delay]
 * @return {Function} 
 */
function debounce(func, delay = 1000) {
    let timer
    return function () {
        if (timer) clearTimeout(timer)
        timer = setTimeout(func, delay)
    }
}

/**
 * 节流
 * @param {Function} func
 * @param {Number} [delay]
 * @return {Function} 
 */
function throttle(func, delay = 1000) {
    let pre = 0
    return function () {
        let now = Date.now()
        if (now - pre > delay) {
            func()
            pre = now
        }
    }
}

/**
 * 使用正则表达式校验字符串【待验证】
 * @param {String} str
 * @param {String} type
 * @return {Boolean} 
 */

function testStr(str = '', type) {
    if (!type) throw new Error('Lack of type argument.');
    let reg = new RegExp();
    switch (type) {
        case 'phone':
            reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
            break;
        case 'email':
            reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
            break;
        case 'idCard':
            reg = /^[1-9]d{5}(18|19|([23]d))d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)d{3}[0-9Xx]$/
            break;
        default:
            break;
    }
    return reg.test(str)
}

export default {
    isIE,
    isEdge,
    isAndroid,
    isIOS,
    isChrome,
    isFF,
    isMobile,
    hello,
    getType,
    formatNum,
    randomStr,
    randomColor,
    parseUrlParams,
    generateUrlParams,
    once,
    debounce,
    throttle,
    testStr,
}